package com.b2b.aliyun.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.b2b.aliyun.AliOSSClient;
import com.b2b.utils.Tools;

public class AliOSSClientImpl implements AliOSSClient
{
	private static final Logger logger = LoggerFactory.getLogger(AliOSSClientImpl.class);
	
	private final static int BUFSIZE = 1024 * 1024;

	private String endpoint;
	private String accessKey;
	private String accessSecret;
	private String bucketName;
	private ClientConfiguration conf = new ClientConfiguration();

	private String getKeyName()
	{
		return Tools.formatDate(System.currentTimeMillis(), "yyyy/MM/dd/") + Tools.genUUID();
	}
	
	@Override
	public String putObject(String prefix, File file)
	{
		return this.putObject(bucketName, prefix, file);
	}

	@Override
	public String putObject(String bucketName, String prefix, File file)
	{
		OSSClient client = new OSSClient(endpoint, accessKey, accessSecret, conf);
		String key = getKeyName();
		
		try
		{
			client.putObject(bucketName, prefix + key, file);
		}
		catch (Exception e)
		{
			logger.error("", e);
		}
		finally
		{
			client.shutdown();
		}
		
		return key;
	}

	@Override
	public String putObject(String prefix, InputStream input)
	{
		return this.putObject(bucketName, prefix, input);
	}

	@Override
	public String putObject(String bucketName, String prefix, InputStream input)
	{
		OSSClient client = new OSSClient(endpoint, accessKey, accessSecret, conf);
		String key = getKeyName();

		try
		{
			client.putObject(bucketName, prefix + key, input);
		}
		catch (Exception e)
		{
			logger.error("", e);
		}
		finally
		{
			client.shutdown();
		}
		
		return key;
	}

	@Override
	public byte[] getObject(String prefix, String key)
	{
		return this.getObject(bucketName, prefix, key);
	}

	@Override
	public byte[] getObject(String bucketName, String prefix, String key)
	{
		OSSClient client = new OSSClient(endpoint, accessKey, accessSecret, conf);
		ByteArrayOutputStream out = new ByteArrayOutputStream();

		try
		{
			OSSObject obj = client.getObject(bucketName, prefix + key);
			if (obj != null)
			{
				InputStream in = obj.getObjectContent();
				byte[] buffer = new byte[BUFSIZE];
				int len = -1;
				while ((len = in.read(buffer)) != -1)
				{
					out.write(buffer, 0, len);
				}
				out.flush();

				return out.toByteArray();
			}
		}
		catch (Exception e)
		{
			logger.error("", e);
		}
		finally
		{
			try
			{
				client.shutdown();
				out.close();
			}
			catch (IOException e)
			{
				logger.error("", e);
			}
		}

		return null;
	}

	public String getEndpoint()
	{
		return endpoint;
	}

	public void setEndpoint(String endpoint)
	{
		this.endpoint = endpoint;
	}

	public String getAccessKey()
	{
		return accessKey;
	}

	public void setAccessKey(String accessKey)
	{
		this.accessKey = accessKey;
	}

	public String getAccessSecret()
	{
		return accessSecret;
	}

	public void setAccessSecret(String accessSecret)
	{
		this.accessSecret = accessSecret;
	}

	public String getBucketName()
	{
		return bucketName;
	}

	public void setBucketName(String bucketName)
	{
		this.bucketName = bucketName;
	}

	public ClientConfiguration getConf()
	{
		return conf;
	}

	public void setConf(ClientConfiguration conf)
	{
		this.conf = conf;
	}
}
